package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.domain.TbRepair;
import com.bwie.domain.TbType;
import com.bwie.mapper.TbRepairMapper;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.service.IUserService;
import com.bwie.service.TbRepairService;
import com.bwie.utils.R;
import com.bwie.vo.DataListVo;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class TbRepairServiceImpl implements TbRepairService {
    @Autowired
    private TbRepairMapper tbRepairMapper;
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private TbTypeMapper tbTypeMapper;
    @Override
    public R getRepairByCountWeekTwo() {
        //查询数据
        List<DataListVo> list = tbRepairMapper.getRepairCountByWeek();
        //构造
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> xList = new ArrayList<>();
        ArrayList<Double> yList = new ArrayList<>();
        for (DataListVo dataListVo : list) {
            xList.add(dataListVo.getName());
            yList.add(dataListVo.getValue());
        }
        map.put("xList",xList);
        map.put("yList",yList);
        return R.OK(map);
    }

    @Override
    public R listPageRepair(PageInfoVo pageInfoVo) {
        Page<TbRepair> tbRepairPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<TbRepair> tbRepairPage1 = null;
        //获取用户角色信息
        String token = httpServletRequest.getHeader("token");
        if (token==null){
            return R.ERROR(500,"用户未登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer userId = body.get("userId", Integer.class);
        R userById = iUserService.getUserById(userId);
        if (userById.getCode() != 200){
            return userById;
        }
        String data = JSON.toJSONString(userById.getData());
        if (JSON.parseObject(data).getInteger("userRole") == 1){
            tbRepairPage1 = tbRepairMapper.getRepairList(tbRepairPage,null,pageInfoVo.getKeyWord(),pageInfoVo.getTypeId(),pageInfoVo.getStatus());
        }else {
            tbRepairPage1 = tbRepairMapper.getRepairList(tbRepairPage,userId,pageInfoVo.getKeyWord(),pageInfoVo.getTypeId(),pageInfoVo.getStatus());
        }

        //查询列表
        return R.OK(tbRepairPage1);
    }

    @Override
    public R gerTypeList() {
        //获取所有类目
        List<TbType> tbTypes = tbTypeMapper.selectList(null);
        return R.OK(tbTypes);

    }
}
